package chapter06

/**
 *
 * @author cdhuangchao3
 * @date 2023/6/9 7:21 PM
 */
object Test12_Singleton {
  def main(args: Array[String]): Unit = {
    val student1 = Student12.getInstance()
    val student2 = Student12.getInstance()
    println(student1)
    println(student2)
  }
}

class Student12 private(val name: String, val age: Int) {
  def printInfo(): Unit = {
    println(s"Student: name= ${name} age= ${age} stdNo=${Student11.school}")
  }
}

// 饿汉式
//object Student12 {
//  private val student: Student12 = new Student12("zs", 18)
//
//  def getInstance(): Student12 = student
//}

// 懒汉式
object Student12 {
  private var student: Student12 = _

  def getInstance(): Student12 = {
    if (student == null) {
      Student12.synchronized {
        if (student == null) {
          student = new Student12("zs", 20)
        }
      }
    }
    student
  }
}